package com.youi.third.wechat.util;


import com.youi.util.FileUploadUtil;
import com.youi.util.LogUtil;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

public class DownloadImgUtil {

	
	/**
	   * 根据内容类型判断文件扩展名
	   *
	   * @param contentType 内容类型
	   * @return
	   */
	  public static String getFileexpandedName(String contentType) {
	    String fileEndWitsh = "";
	    if ("image/jpeg".equals(contentType))
	      fileEndWitsh = ".jpg";
	    else if ("audio/mpeg".equals(contentType))
	      fileEndWitsh = ".mp3";
	    else if ("audio/amr".equals(contentType))
	      fileEndWitsh = ".amr";
	    else if ("video/mp4".equals(contentType))
	      fileEndWitsh = ".mp4";
	    else if ("video/mpeg4".equals(contentType))
	      fileEndWitsh = ".mp4";
	    return fileEndWitsh;
	  }
	  /**
	   * 获取媒体文件
	   * @param accessToken 接口访问凭证
	   * @param mediaId 媒体文件id
	   * @param savePath 文件在本地服务器上的存储路径
	   * */
	  public static String downloadMedia(HttpServletRequest request,String mediaId) {
	    String accessToken = WechatUtil.getAccessToken();
	    if (accessToken!=null) {
	    	 String filePath = null;
	 	    // 拼接请求地址
	 	    String requestUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	 	    requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("MEDIA_ID", mediaId);
	 	    try {
	 	      URL url = new URL(requestUrl);
	 	      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	 	      conn.setDoInput(true);
	 	      conn.setRequestMethod("GET");
	 	      
	 	      // 根据内容类型获取扩展名
	 	      String fileExt = getFileexpandedName(conn.getHeaderField("Content-Type"));
	 	      //将mediaId作为文件名
	 	      filePath = FileUploadUtil.getFileUploadUrl(request, mediaId+fileExt) ;
	 	      BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
	 	      FileOutputStream fos = new FileOutputStream(new File(filePath));
	 	      byte[] buf = new byte[8096];
	 	      int size = 0;
	 	      while ((size = bis.read(buf)) != -1){
		 	       fos.write(buf, 0, size);
	 	      }
	 	      fos.close();
	 	      bis.close();
	 	      conn.disconnect();
	 	      String info = String.format("下载媒体文件成功，filePath=" + filePath);
	 	      LogUtil.Info(info);
	 	    } catch (Exception e) {
	 	      filePath = null;
	 	      String error = String.format("下载媒体文件失败：%s", e);
	 	      LogUtil.Info(error);
	 	    }
	 	   return filePath;
		}
	    return null;
	  }
	
}
